
#ifndef INCLUDE_IO_POLL
#define INCLUDE_IO_POLL

#include "io_reuse_module_impl.h"
#include <cstdint>
#include <memory>
#include <sys/poll.h>
#include <vector>
#include <map>

class IoPoll : std::enable_shared_from_this<IoPoll>, public IoReuseModuleImpl
{
public:
    explicit IoPoll(int size = 60000);
    virtual ~IoPoll() override;
    int AddPollEvent(struct pollfd & event);
    int ModPollEvent(struct pollfd & event);
    int DelPollEvent(struct pollfd & event);
    /* int PollWait(); */
    int PollWait(std::vector<struct pollfd> & poll_vec, int wait_time);
public:
    int64_t AddEvent(int description, int event) override;
    int64_t ModEvent(int64_t id, int event) override;
    int64_t DelEvent(int64_t id) override;
    int WaitEvent(std::vector<struct EventData> & trigger_data, int wait_time) override;
private:
    int64_t m_index;
    std::map<int64_t, struct pollfd> m_poll_map;
    std::map<int, int64_t> m_fd_index_map;
    std::vector<struct pollfd> m_poll_vec;
};

class IoPollFactory : public IoReuseModuleFactory
{
public:
    std::shared_ptr<IoReuseModuleImpl> CreateModule() override;
};

#endif
